package com.start.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.admin.entity.StartSysDictionary;
import com.start.admin.service.StartSysDictionaryService;
import com.start.common.api.Result;
import com.start.common.dataImport.ImportExportUtils;
import com.start.common.dataRule.DataQueryConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/start/sysDictionary")
public class StartSysDictionaryController {

    @Autowired
    private StartSysDictionaryService startSysDictionaryService;

    @GetMapping("/page")
    public Result page(Page page, StartSysDictionary entity) {
        LambdaQueryWrapper<StartSysDictionary> lambdaQuery = DataQueryConstructor.createEntity(entity).lambda();
        return Result.ok(startSysDictionaryService.page(page, lambdaQuery));
    }

    @PostMapping("/add")
    @PreAuthorize("@pas.hasPermission('sys:dictionary:add')")
    public Result add(@RequestBody StartSysDictionary entity) {
        startSysDictionaryService.save(entity);
        return Result.ok();
    }

    @PutMapping("/updateById")
    @PreAuthorize("@pas.hasPermission('sys:dictionary:edit')")
    public Result updateById(@RequestBody StartSysDictionary entity) {
        startSysDictionaryService.updateById(entity);
        return Result.ok();
    }

    @GetMapping("/getById/{id}")
    @PreAuthorize("@pas.hasPermission('sys:dictionary:info')")
    public Result getById(@PathVariable("id") String id) {
        return Result.ok(startSysDictionaryService.getById(id));
    }


    @DeleteMapping("deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('sys:dictionary:delete')")
    public Result<?> deleteById(@PathVariable("id") String id) {
        startSysDictionaryService.removeById(id);
        return Result.ok();
    }

    @PostMapping("/importFile")
    public Result<?> importFile(@RequestParam("file") MultipartFile file) {
        return Result.ok();
    }

    @GetMapping("/exportFile")
    public void exportFile(StartSysDictionary entity, HttpServletResponse response) {
        String fileName = "字典" + System.currentTimeMillis() + ".xls";
        LambdaQueryWrapper<StartSysDictionary> lambdaQuery = Wrappers.lambdaQuery(entity);
        List<StartSysDictionary> list = startSysDictionaryService.list(lambdaQuery);
        ImportExportUtils.exportExcel(StartSysDictionary.class, list, response, fileName);

    }

    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "字典导入模板.xls";
        try {
            ImportExportUtils.createImportTemplate(StartSysDictionary.class, response, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
